{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典、集合、序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "字符串属于序列类型\n",
    "可变类型指的是，增加或删减元素前后，内存地址不改变。不可变类型与此相反。\n",
    "列表、集合、字典属于可变类型\n",
    "数值、字符和元组属于不可变类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "字典内部存放的顺序和key放入的前后顺序无关"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Just do it'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic = {'李宁': '一切皆有可能', '耐克': 'Just do it', '阿迪达斯': 'Impossible is nothing'}\n",
    "dic['耐克']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "brand = ['李宁', '耐克', '阿迪达斯']\n",
    "slogan = ['一切皆有可能', 'Just do it', 'Impossible is nothing']\n",
    "slogan[brand.index('李宁')]\n",
    "brand.index('李宁')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2, 'c': 3}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建方式一 从空字典中进行创建\n",
    "dic = dict()\n",
    "dic['a'] = 1\n",
    "dic['b'] = 2\n",
    "dic['c'] = 3\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': 4139, 'peach': 4127, 'cherry': 4098}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建方式二 通过元组进行创建  列表中包括元组\n",
    "dic1 = dict([('apple', 4139), ('peach', 4127), ('cherry', 4098)])\n",
    "dic1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Tom', 'age': 10}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建方式三  通过=进行创建\n",
    "dic = dict(name='Tom', age=10)\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': ('小马', '8', '男'), 'age': ('小马', '8', '男'), 'sex': ('小马', '8', '男')}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建方式四  dict.fromkeys(）  创建一个键不相同 值相同的字典\n",
    "seq = ('name', 'age', 'sex')\n",
    "values =  ('小马', '8', '男')\n",
    "dict.fromkeys(seq,values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Name', 'Age']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典的内置方法\n",
    "# 1   dict.keys()返回字典中所有的键,一般需要使用list函数进行转化\n",
    "dic = {'Name': 'lsgogroup', 'Age': 7}\n",
    "list(dic.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['female', 7, 'Zara']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2   dict.values()返回字典中所有的值,一般需要使用list函数进行转化\n",
    "dic = {'Sex': 'female', 'Age': 7, 'Name': 'Zara'}\n",
    "list(dic.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('Name', 'Lsgogroup'), ('Age', 7)])\n",
      "[('Name', 'Lsgogroup'), ('Age', 7)]\n",
      "(('Name', 'Lsgogroup'), ('Age', 7))\n"
     ]
    }
   ],
   "source": [
    "#3   dict.items() 以列表返回可遍历的 (键, 值) 元组数组\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 7}\n",
    "print(dic.items())\n",
    "print(list(dic.items()))\n",
    "print(tuple(dic.items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#4  dic.get()返回指定键的值\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 27}\n",
    "dic.get('Age')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'boy'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#5 dict.setdefault(） 如果查找的键不在 则返回默认值\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 27}\n",
    "dic.setdefault('SEX',\"boy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#6 key in dict 根据键来判断该键是否在字典中\n",
    "#7  dict.pop() 删除字典给定键 key 所对应的值，返回值为被删除的值。 key 值必须给出。若 key 不存在，则返回 default 值。\n",
    "dic1 = {1: \"a\", 2: [1, 2]}\n",
    "dic1.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: [1, 2]}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#8  del dict[key]  直接删除 不返回任何值\n",
    "dic1 = {1: \"a\", 2: [1, 2]}\n",
    "del dic1[1]\n",
    "dic1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, [1, 2])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#9 dict.popitem() 随机返回并删除字典中的一对键和值，把被删除的返回,缺点在于无法指定删哪一个 \n",
    "dic1 = {1: \"a\", 2: [1, 2]}\n",
    "dic1.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#10  dict.clear() 用于删除字典内所有元素，完全清空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1583676595152\n",
      "1583676595152\n",
      "1583676594648\n"
     ]
    }
   ],
   "source": [
    "#11 dict.copy() 返回一个字典的浅复制。\n",
    "dic1 = {'user': 'lsgogroup', 'num': [1, 2, 3]}\n",
    "#直接引用 地址不变\n",
    "dic2 = dic1\n",
    "#copy 地址会改变\n",
    "dic3 = dic1.copy()\n",
    "print(id(dic1)) \n",
    "print(id(dic2)) \n",
    "print(id(dic3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'user': 'root', 'num': [1, 2, 3]}\n",
      "{'user': 'root', 'num': [1, 2, 3]}\n",
      "{'user': 'lsgogroup', 'num': [1, 2, 3]}\n"
     ]
    }
   ],
   "source": [
    "dic1['user'] = 'root'\n",
    "print(dic1) \n",
    "print(dic2) \n",
    "print(dic3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'user': 'root', 'num': [2, 3]}\n",
      "{'user': 'root', 'num': [2, 3]}\n",
      "{'user': 'lsgogroup', 'num': [2, 3]}\n"
     ]
    }
   ],
   "source": [
    "dic1['num'].remove(1)\n",
    "print(dic1) \n",
    "print(dic2) \n",
    "print(dic3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'num': [1, 2, 3]}\n",
      "{'num': [1, 2, 3]}\n",
      "{'user': 'lsgogroup', 'num': [1, 2, 3]}\n"
     ]
    }
   ],
   "source": [
    "dic1 = {'user': 'lsgogroup', 'num': [1, 2, 3]}\n",
    "#直接引用 地址不变\n",
    "dic2 = dic1\n",
    "#copy 地址会改变\n",
    "dic3 = dic1.copy()\n",
    "del dic1[\"user\"]\n",
    "print(dic1) \n",
    "print(dic2) \n",
    "print(dic3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Lsgogroup', 'Age': 8, 'Sex': 'female'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 12 dict.update(dict2) 将第二个字典添加到第一个字典之中 键如果相同 就是起到更新的作用\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 7}\n",
    "dic2 = {'Sex': 'female', 'Age': 8}\n",
    "dic.update(dic2)\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "#练习题1\n",
    "dic = { 'python': 95, 'java': 99, 'c': 100}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'python': 95, 'java': 98, 'c': 100}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic['java'] = 98\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic.pop('c')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'python': 95, 'java': 98, 'php': 90}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic['php'] =90\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python', 'java', 'php']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(dic.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[95, 98, 90]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(dic.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'javascript' in dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "283"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(list(dic.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "98"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(list(dic.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(list(dic.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'python': 95, 'java': 98, 'php': 97}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic1 = {'php': 97}\n",
    "dic.update(dic1)\n",
    "dic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "集合的形式{1,2,3}\n",
    "空集合的创建只能使用s = set()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple', 'banana'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basket = set()\n",
    "basket.add('apple') #add函数网集合中添加元素 只能是单个元素\n",
    "basket.add('banana')\n",
    "basket.add('banana')\n",
    "basket.add('banana')\n",
    "basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Lsgogroup', 'Taobao', 'Google'}\n"
     ]
    }
   ],
   "source": [
    "#set函数可以将字符串、列表或元组转换成集合 也是创建方式的一种\n",
    "c = set([\"Google\", \"Lsgogroup\", \"Taobao\", \"Google\"])\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "由于set存储的是无序集合，不可以为集合创建索引或执行切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "set.update可以添加新的集合到当前集合当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple', 'baidu', 'banana', 'cherry', 'google'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = {\"apple\", \"banana\", \"cherry\"}\n",
    "y = {\"google\", \"baidu\", \"apple\"}\n",
    "x.update(y)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "set.remove(item) 用于移除集合中的指定元素，如果元素不存在，则会发生错误\n",
    "set.discard方法同上，如果不存在。不会报错    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "数学意义上的集合操作\n",
    "\n",
    "set1 & set2 返回两个集合的交集。\n",
    "set1 | set2 返回两个集合的并集。\n",
    "set1 - set2 返回集合的差集。\n",
    "set1 ^ set2 返回集合的异或\n",
    "set1 <= set2 判断集合是不是被其他集合包含，如果是则返回 True，否则返回 False。\n",
    "set1 >= set2 判断集合是不是包含其他集合，如果是则返回 True，否则返回 False。\n",
    "set.isdisjoint(set) 用于判断两个集合是不是不相交，如果是返回 True，否则返回 False。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "集合的转换\n",
    "列表，元组，和集合三者之间可以相互转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "不能改变元素的集合的实现版本，即不能增加或删除元素，类型名叫 frozenset 。需要注意的\n",
    "是 frozenset 仍然可以进行集合操作，只是不能用带有 update 的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "练习题\n",
    "1. 怎么表示只包含⼀个数字1的元组。\n",
    "2. 创建一个空集合，增加 {‘x’,‘y’,‘z’} 三个元素。\n",
    "3. 列表['A', 'B', 'A', 'B']去重。\n",
    "4. 求两个集合{6, 7, 8}，{7, 8, 9}中不重复的元素（差集指的是两个集合交集外的部分）。\n",
    "5. 求{'A', 'B', 'C'}中元素在 {'B', 'C', 'D'}中出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((1,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x', 'y', 'z'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set()\n",
    "s.add('x')\n",
    "s.add('y')\n",
    "s.add('z')\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['B', 'A']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表快速去重方法 转换为集合\n",
    "list(set(['A', 'B', 'A', 'B']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{6, 9}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#集合的异或操作\n",
    "{6, 7, 8}^{7, 8, 9}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len({'A', 'B', 'C'}&{'B', 'C', 'D'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len不仅可以返回对象的元素个数，还能返回对象的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-8, 3, 7, 83, 99]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [-8, 99, 3, 7, 83]\n",
    "sorted(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ({\"age\": 20, \"name\": \"a\"}, {\"age\": 25, \"name\": \"b\"}, {\"age\": 10, \"name\": \"c\"})\n",
    "x = sorted(t, key=lambda a: a[\"age\"]) #for a in t\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reversed 快速对可迭代对象实现反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<enumerate object at 0x000001E6A1A709D8>\n",
      "[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\n"
     ]
    }
   ],
   "source": [
    "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n",
    "print(enumerate(seasons)) #如果不转换成list的形式 不会输出任何有用的值\n",
    "print(list(enumerate(seasons)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1  Spring\n",
      "2  Summer\n",
      "3  Fall\n",
      "4  Winter\n"
     ]
    }
   ],
   "source": [
    "a = list(enumerate(seasons,1))\n",
    "for i,j in a:\n",
    "    print('{0}  {1}'.format(i,j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "zip函数 \n",
    "同enumberate一样 都需要转换为列表形式再输出出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2, 3]\n",
    "b = [4, 5, 6]\n",
    "c = [4, 5, 6, 7, 8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 4), (2, 5), (3, 6)]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 4), (2, 5), (3, 6)]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(a,c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "(4, 5, 6)\n"
     ]
    }
   ],
   "source": [
    "# 解压使用*\n",
    "i,j = zip(*zip(a,b)) \n",
    "print(i)\n",
    "print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "练习题\n",
    "1. 怎么找出序列中的最⼤、⼩值？\n",
    "2. sort() 和 sorted() 区别\n",
    "3. 怎么快速求 1 到 100 所有整数相加之和？\n",
    "4. 求列表 [2,3,4,5] 中每个元素的立方根。\n",
    "5. 将[‘x’,‘y’,‘z’] 和 [1,2,3] 转成 [(‘x’,1),(‘y’,2),(‘z’,3)] 的形式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2 \n",
    "sorted可以对列表元组 字典 字符串进行排序，排序对象作为参数传入\n",
    "sort()只能对列表排序，sort()对列表排序时，无法返回值（直接在原列表中操作）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3\n",
    "sum(list(range(1,101)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8, 27, 64, 125]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4\n",
    "a = []\n",
    "for i in [2,3,4,5]:\n",
    "    a.append(i**3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "#5   [(‘x’,1),(‘y’,2),(‘z’,3)]\n",
    "a = ['x','y','z'] \n",
    "b = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('x', 1), ('y', 2), ('z', 3)]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(a,b))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
